package controllers;

import com.google.gson.JsonObject;

import models.Entities;
import models.IdeaDeveloper;
import models.Organization;
import models.OrganizationLead;
import models.Organizer;
import play.mvc.Controller;
import play.test.Fixtures;

public class User extends Controller {

	/**
	 * renders the page organizationLead
	 * 
	 * @author omarkhaled
	 */
	public static void OrganizationLead() {
		Fixtures.deleteAll();
		IdeaDeveloper usercur = new IdeaDeveloper("omar", "omar khaled",
				"11111111", "omar@yahoo", "18/1/1990", "3 street");
		usercur.save();
		OrganizationLead orglead = new OrganizationLead();
		orglead.ideaDeveloper = usercur;
		orglead.save();
		IdeaDeveloper userOnP = new IdeaDeveloper("khaled", "khaled omar",
				"11111111", "khaled@yahoo", "18/1/1990", "3 street");
		userOnP.save();
		Organization org = Organization.create(orglead, "Dih Organizaion");
		org.save();
		Entities ent = Entities.create(org).save();
		ent.name = "Dih Entity";
		ent.save();
		String organizationName = org.name;
		String userCurrent = usercur.username;
		String userOnPage = userOnP.username;
		String entityName = ent.name;
		render(userOnPage, organizationName, userCurrent, entityName);
	}

	/**
	 * renders the page ideadev
	 * 
	 * @author omarkhaled
	 */
	public static void IdeaDev() {
		Fixtures.deleteAll();
		IdeaDeveloper usercur = new IdeaDeveloper("omar", "omar khaled",
				"11111111", "omar@yahoo", "18/1/1990", "3 street");
		usercur.save();
		OrganizationLead orglead = new OrganizationLead();
		orglead.ideaDeveloper = usercur;
		orglead.save();
		IdeaDeveloper userOnP = new IdeaDeveloper("khaled", "khaled omar",
				"11111111", "khaled@yahoo", "18/1/1990", "3 street");
		userOnP.save();
		Organization org = Organization.create(orglead, "Dih Organizaion");
		String organizationName = org.name;
		String userCurrent = usercur.username;
		String userOnPage = userOnP.username;
		render(userOnPage, organizationName, userCurrent);
	}

	/**
	 * renders the page where the Organization Leader can invite idea developers
	 * to organization
	 * 
	 * @author OmarKhaled
	 * @param userOnPage
	 *            the username of the idea developer to be invited
	 * @param organizationName
	 *            the organization where the action takes place
	 */
	public static void inviteIdeaDeveloperToOrganization(String userOnPage,
			String organizationName) {
		IdeaDeveloper invited = (IdeaDeveloper) IdeaDeveloper.find(
				"username = ?", userOnPage).first();
		Organization org = (Organization) Organization.find("name = ?",
				organizationName).first();
		OrganizationLead.inviteIdeaDeveloperToOrganization(org, invited,
				"JoinOrgFromOrgToDev");
		JsonObject inviteToOrgJson = new JsonObject();
		inviteToOrgJson.addProperty("result", "done");
		renderJSON(inviteToOrgJson.toString());
	}

	/**
	 * renders the page where the Organization Leader can invite idea developers
	 * to entity
	 * 
	 * @author OmarKhaled
	 * @param userOnPage
	 *            the username of the idea developer to be invited
	 * @param entityName
	 *            the entity where the action takes place
	 */
	public static void inviteIdeaDeveloperToEntity(String userOnPage,
			String entityName) {
		IdeaDeveloper invited = (IdeaDeveloper) IdeaDeveloper.find(
				"username = ?", userOnPage).first();
		Entities ent = (Entities) Entities.find("name = ?", entityName).first();
		OrganizationLead.inviteIdeaDeveloperToEntity(ent, invited,
				"JoinOrgFromEnyToDev");
		JsonObject inviteToEntJson = new JsonObject();
		inviteToEntJson.addProperty("result", "done");
		renderJSON(inviteToEntJson.toString());
	}

	/**
	 * renders the page where the organization lead can block idea developer
	 * from organization
	 * 
	 * @author OmarKhaled
	 * @param userOnPage
	 *            user which will be blocked
	 * @param organizationName
	 *            the organization where the action takes place
	 * @param userCurrent
	 *            organization leader which blocking the user
	 * @param BlockReason
	 *            the statement which will be sent as notification to the
	 *            organizer as a reason of block
	 */
	public static void blockFromOrganization(String userOnPage,
			String organizationName, String userCurrent, String BlockReason) {
		IdeaDeveloper userBlocked = (IdeaDeveloper) IdeaDeveloper.find(
				"username = ?", userOnPage).first();
		Organization org = (Organization) Organization.find("name = ?",
				organizationName).first();
		OrganizationLead orgLead = new OrganizationLead();
		orgLead.ideaDeveloper = (IdeaDeveloper) IdeaDeveloper.find(
				"username = ?", userCurrent).first();
		OrganizationLead.blockFromOrganization(org, orgLead, userBlocked,
				BlockReason);
		JsonObject blockFromOrgJson = new JsonObject();
		blockFromOrgJson.addProperty("result", "done");
		renderJSON(blockFromOrgJson.toString());
	}

	/**
	 * renders the page where the organization lead can unblock idea developer
	 * from organization
	 * 
	 * @author OmarKhaled
	 * @param userOnPage
	 *            user which will be unblocked
	 * @param organizationName
	 *            the organization where the action takes place
	 * @param userCurrent
	 *            organization leader which unblocking the user
	 * @param unBlockReason
	 *            the statement which will be sent as notification to the
	 *            organizer as a reason of unblock
	 */
	public static void unBlockFromOrganization(String userOnPage,
			String organizationName, String userCurrent, String unBlockReason) {
		IdeaDeveloper userUnBlocked = (IdeaDeveloper) IdeaDeveloper.find(
				"username = ?", userOnPage).first();
		Organization org = (Organization) Organization.find("name = ?",
				organizationName).first();
		OrganizationLead orgLead = new OrganizationLead();
		orgLead.ideaDeveloper = (IdeaDeveloper) IdeaDeveloper.find(
				"username = ?", userCurrent).first();
		OrganizationLead.unBlockFromOrganization(org, orgLead, userUnBlocked,
				unBlockReason);
		JsonObject unBlockFromOrgJson = new JsonObject();
		unBlockFromOrgJson.addProperty("result", "done");
		renderJSON(unBlockFromOrgJson.toString());
	}

	/**
	 * renders the page where the organization lead can invite/assign any idea
	 * developer to be organizer on entity
	 * 
	 * @author OmarKhaled
	 * @param userOnPage
	 *            idea developer to be assigned as organizer
	 * @param entityName
	 *            entity where idea developer will be organizer on
	 */
	public static void invitationToBeOrganizer(String userOnPage,
			String entityName) {
		IdeaDeveloper invited = (IdeaDeveloper) IdeaDeveloper.find(
				"username = ?", userOnPage).first();
		Entities entity = (Entities) Entities.find("name = ?", entityName)
				.first();
		Organizer.invitationToBeOrganizer(entity, invited,
				"JoinEntFromEntToDev");
		JsonObject inviteToBeOrgJson = new JsonObject();
		inviteToBeOrgJson.addProperty("result", "done");
		renderJSON(inviteToBeOrgJson.toString());
	}

	/**
	 * renders the function which checks if the user"viewer" can view the
	 * profile page of the user "to be viewed"
	 * 
	 * @author OmarKhaled
	 * @param userOnPage
	 *            the username of the idea developer which profile will be
	 *            viewed
	 * @param userCurrent
	 *            the username of the idea developer requesting to view the
	 *            profile
	 */
	public static void canViewProfile(String userOnPage, String userCurrent) {
		IdeaDeveloper toBeViewedDev = (IdeaDeveloper) IdeaDeveloper.find(
				"username = ?", userOnPage).first();
		IdeaDeveloper viewerDev = (IdeaDeveloper) IdeaDeveloper.find(
				"username = ?", userCurrent).first();
		IdeaDeveloper.canViewProfile(toBeViewedDev, viewerDev);
		JsonObject viewProfile = new JsonObject();
		viewProfile.addProperty("result", "done");
		renderJSON(viewProfile.toString());
	}
}
